<?php

namespace SinoPHP\BbdBoss\Api;

/**
 * 服务器组同步接口
 *
 */
class ServerGroup extends ApiAbstract {

    public function v_1_0_0() {

        $params = $this->requestParams;

        //$this->checkToken(); // 校验TOKEN

        //获取服务器组id code
        $idList = model('ServerGroup')->getList('server_group_id,server_code',['status' => 1]);
        //获取服务器组id
        $id = array_column($idList,'server_group_id');
        //获取ServerGroupIp关联表的所有 ip_id 服务器id
        $res = model('ServerGroupIp')->getList('ip_id,server_group_id',['server_group_id' => $id]);
        //获取ip_id
        $ipId = array_column($res,'ip_id');
        //获取关联表的ip_id ip
        $data = model('ServerIp')->getList('ip_id,ip',['ip_id' => $ipId]);
        //拼接
        $ipData = [];
        foreach ($data as $value){
            $ipData[$value['ip_id']] = $value['ip'];
        }
        $resSort = [];
        foreach ($res as $value){
            $resSort[$value['server_group_id']][] = $ipData[$value['ip_id']];
        }
        foreach ($idList as $key => $item) {
            $idList[$key]['server_group_id'] = intval($item['server_group_id']);
            $serverIps = $resSort[$item['server_group_id']] ? : [];
            $idList[$key]['serverIps'] = array_filter($serverIps);
        }

        $this->success($idList);
    }

}